package com.softwinner.bt.adapter;

import java.util.ArrayList;

import com.softwinner.bt.R;
import com.softwinner.bt.bean.BtContact;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class ContactAdapter extends BaseAdapter {
    private Context mContext;
    private LayoutInflater mInflater;
    private ArrayList<com.softwinner.bt.bean.BtContact> contacts;
    private int mSelectPosition = -1;

    public ContactAdapter(Context context, ArrayList<BtContact> contacts) {
        this.mContext = context;
        this.contacts = contacts;
        mInflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return contacts.size();
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return contacts.get(position);
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        ViewHolder viewHolder;
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.list_item, null);
            viewHolder = new ViewHolder();
            viewHolder.nameTex = (TextView) convertView.findViewById(R.id.list_item_label);
            viewHolder.numTex = (TextView) convertView.findViewById(R.id.list_item_sublabel);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        if(position == mSelectPosition){
        	convertView.setBackgroundResource(R.drawable.list_item_bg);
        }else{
        	convertView.setBackgroundColor(Color.TRANSPARENT);
        }
        viewHolder.nameTex.setText(contacts.get(position).getName());
        viewHolder.numTex.setText(contacts.get(position).getNumber());        
        return convertView;
    }
    
    public void setSelected(int p){
    	mSelectPosition = p;
    }

    public void setDataChange(ArrayList<BtContact> contacts) {
        this.contacts = contacts;
        this.notifyDataSetChanged();
    }

    private class ViewHolder {
        TextView nameTex;
        TextView numTex;
    }

}
